package com.handinfo.android;

import java.io.IOException;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.RectF;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.util.AttributeSet;
import android.util.Log;

import com.handinfo.android.core.graphics.opengl.Texture2D;

public class DWGLSurfaceView extends GLSurfaceView implements Renderer
{
	private Bitmap[] bitmaps = new Bitmap[2];
	private Context m_context;

	public DWGLSurfaceView(Context context)
	{
		super(context);
		setRenderer(this);
		m_context = context;
	}

	public DWGLSurfaceView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
	}

	private void initGL(GL10 gl)
	{
//      assert openGLView_ != null : "openGLView_ must be initialized";

		setAlphaBlending(gl, true);
		gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA);

		setDepthTest(gl, false);

		gl.glDisable(gl.GL_LIGHTING);

		gl.glShadeModel(gl.GL_FLAT);
//
//      setTexture2D(gl, true);

		// set other opengl default values
		gl.glClearColor(0.0f, 0.0f, 0.0f, 1f);

	}

	public void setAlphaBlending(GL10 gl, boolean on)
	{
		if (on)
			gl.glEnable(gl.GL_BLEND);
		else
			gl.glDisable(gl.GL_BLEND);
	}

	public void setDepthTest(GL10 gl, boolean on)
	{
		if (on)
			gl.glEnable(gl.GL_DEPTH_TEST);
		else
			gl.glDisable(gl.GL_DEPTH_TEST);
	}

	public void onDrawFrame(GL10 gl1)
	{
		gl1.glClear(GL10.GL_COLOR_BUFFER_BIT);
		gl1.glClearColor(0, 0, 1.0f, 1.0f);

		RectF rect = new RectF();
		long startTime = System.currentTimeMillis();
		for (int i = 0; i < 500; i++) {
			m_texture.draw(gl1, rect);
		}
		Log.e("绘制时间", String.valueOf(System.currentTimeMillis() - startTime));
	}

	public void onSurfaceChanged(GL10 arg0, int arg1, int arg2)
	{
		arg0.glViewport(0, 0, arg1, arg2);
	}

	int[] textures;

	Texture2D m_texture;

	public void onSurfaceCreated(GL10 gl, EGLConfig arg1)
	{
//		initGL(gl1);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();
		gl.glScalef(1, -1, 1);
//		gl.glOrthof(-400.0f, 400.0f, -240.0f, 240.0f, -1, 1);
//		GLU.gluLookAt(gl, 0, 0, 100.0f, 0, 0, 0, 0, 1.0f, 0);
		try {
//			Bitmap bitmap2 = BitmapFactory.decodeStream(m_context.getAssets().open("img/login_lianhua.gnp"));
//			bitmap = get2Bitmap(bitmap2);
//			bitmap = BitmapFactory.decodeStream(m_context.getAssets().open("img/icon.gnp"));
			bitmaps[0] = BitmapFactory.decodeStream(m_context.getAssets().open("img/000.gnp"));
			bitmaps[1] = BitmapFactory.decodeStream(m_context.getAssets().open("img/icon.gnp"));

		}
		catch (IOException e) {
			e.printStackTrace();
		}

		m_texture = new Texture2D(bitmaps[1], new Rect(0, 0, 48, 48));

//		gl.glDisable(GL10.GL_DITHER);
//		gl.glClearColor(0.5f, 0.5f, 0.5f, 1);
//		gl.glShadeModel(GL10.GL_SMOOTH);
//		gl.glEnable(GL10.GL_CULL_FACE);
//		gl.glCullFace(GL10.GL_BACK);
		gl.glEnable(GL10.GL_DEPTH_TEST);
//		gl.glDisable(GL10.GL_LIGHTING);
//		gl.glDisable(GL10.GL_BLEND);
	}

	public static int pow2(int size)
	{
		int small = (int) (Math.log((double) size) / Math.log(2.0f));
		if ((1 << small) >= size)
			return 1 << small;
		else
			return 1 << (small + 1);
	}

	public Bitmap get2Bitmap(Bitmap bmp)
	{
		// mBitmap = bmp;
		int mWidth = bmp.getWidth();
		int mHeight = bmp.getHeight();

		int mPow2Height = pow2(mHeight);
		int mPow2Width = pow2(mWidth);

		float maxU = mWidth / (float) mPow2Width;
		float maxV = mHeight / (float) mPow2Height;

		Bitmap bitmap = Bitmap.createBitmap(mPow2Width, mPow2Height,
				bmp.hasAlpha() ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
		Canvas canvas = new Canvas(bitmap);
		canvas.drawBitmap(bmp, 0, 0, null);
		return bitmap;
	}
}
